package com.example.amoxicilin.null_test.main;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.CardView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.amoxicilin.null_test.R;
import com.example.amoxicilin.null_test.domain.User;
import com.example.amoxicilin.null_test.tools.CodeTools;
import com.example.amoxicilin.null_test.tools.HttpUtils;
import com.example.amoxicilin.null_test.tools.JsonTools;

import java.io.UnsupportedEncodingException;


public class MeFragment extends Fragment {

    public static final int SHOW_RESPONSE = 0;
    static  String person;
    int usernumber;
    private String content = "";
    CardView cardView;
    private User user;
    private TextView name;
    private TextView intro;
    private TextView college;
    private CardView password;
    private CardView editinfo;
    private CardView mQuestion;
    private CardView mAnswer;
    private CardView mDiary;
    ImageView img1,img2,img3;


    private Handler handler;

    {
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                String response = (String) msg.obj;
                user = JsonTools.getUser("user", response);
                name.setText(user.getName());
                intro.setText(user.getIntroduction());
                //college.setText(user.getCollege());
                img1=(ImageView)getActivity().findViewById(R.id.label1);
                img2=(ImageView)getActivity().findViewById(R.id.label2);

                switch (user.getLabel1()){
                    case 1:
                        img1.setImageResource(R.drawable.huaxue);break;
                    case 2:
                        img1.setImageResource(R.drawable.jisuanji);break;

                    default:break;
                }
                switch (user.getLabel2()){
                    case 1:
                        img2.setImageResource(R.drawable.huaxue);break;
                    case 2:
                        img2.setImageResource(R.drawable.jisuanji);break;

                    default:break;
                }
            }
        };
    }
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootview=inflater.inflate(R.layout.fragment_me,container, false);
        cardView =(CardView) rootview.findViewById(R.id.card_view_info);
        name=(TextView)rootview.findViewById(R.id.me_name);
        intro=(TextView)rootview.findViewById(R.id.me_intro);
        college=(TextView)rootview.findViewById(R.id.me_college);
        password=(CardView)rootview.findViewById(R.id.me_password);
        editinfo=(CardView)rootview.findViewById(R.id.me_edit_info);
        mQuestion=(CardView)rootview.findViewById(R.id.me_question);
        mAnswer=(CardView)rootview.findViewById(R.id.me_answer);
        mDiary=(CardView)rootview.findViewById(R.id.me_diary);
        final Bundle bundle=getArguments();
        person="TOM";
        usernumber=bundle.getInt("loginnumber");

        password.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent =new Intent();
                intent.setClass(getActivity(),PassWordActivity.class);
                Bundle passwordbundle=new Bundle();
                passwordbundle.putString("password",user.getPassword());
                passwordbundle.putInt("number",user.getNumber());
                intent.putExtras(passwordbundle);
                startActivity(intent);
            }
        });

        editinfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent =new Intent();
                intent.setClass(getActivity(),InfoActivity.class);
                Bundle infobundle=new Bundle();
                infobundle.putInt("number",user.getNumber());
                intent.putExtras(infobundle);
                startActivity(intent);
            }
        });

        mQuestion.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent =new Intent();
                intent.setClass(getActivity(),MyQuestionActivity.class);
                Bundle Questionbundle=new Bundle();
                Questionbundle.putInt("usernumber",user.getNumber());
                intent.putExtras(Questionbundle);
                startActivity(intent);
            }
        });

        mAnswer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent =new Intent();
                intent.setClass(getActivity(),MyAnswerActivity.class);
                Bundle Answerbundle=new Bundle();
                Answerbundle.putInt("usernumber",user.getNumber());
                intent.putExtras(Answerbundle);
                startActivity(intent);
            }
        });

        mDiary.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(getActivity(),MyDiaryActivity.class);
                Bundle Diarybundle = new Bundle();
                Diarybundle.putInt("usernumber",user.getNumber());
                intent.putExtras(Diarybundle);
                startActivity(intent);
            }
        });
        initData();

        return rootview;

    }
    private void initData(){
        try {
            content = CodeTools.UTF8Encoder(person.toString());
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String path = "http://192.168.12.221:8080/tmoocserver/LoginServlet?name=" + "uncleb" + "&password=" + "123456";
        String jsonString = HttpUtils.getJsonContent(path);
        Log.i("aaa",jsonString);
        Message message= new Message();
        message.what=SHOW_RESPONSE;
        message.obj=jsonString;
        handler.sendMessage(message);
    }
}

